import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _DataSet from "choerodon-ui/pro/lib/data-set";
import _isArray from "lodash/isArray";
import _isUndefined from "lodash/isUndefined";
import _isString from "lodash/isString";
import _map from "lodash/map";
import React, { createContext, useMemo } from 'react';
import SortDataSet from "./SortDataSet";
import QueryDataSet from "./QueryDataSet";
import ConditionDataSet from "./ConditionDataSet";
import SearchDataSet from "./SearchDataSet";
var Store = createContext();
export default Store;
export var StoreProvider = function StoreProvider(props) {
  var children = props.children,
      tableDataSet = props.tableDataSet,
      fields = props.fields,
      _props$ignoreFields = props.ignoreFields,
      ignoreFields = _props$ignoreFields === void 0 ? [] : _props$ignoreFields,
      searchCode = props.searchCode;
  var fieldArr = [];
  var fieldsValue = [];

  if (fields && _isArray(fields)) {
    fieldsValue = fields;
  } else {
    fieldsValue = Object.values(_map(tableDataSet.fields.toJSON()));
  }

  fieldArr = _map(fieldsValue, function (field) {
    var _ref = field.props || field,
        type = _ref.type,
        name = _ref.name,
        lookupUrl = _ref.lookupUrl,
        lovCode = _ref.lovCode,
        lookupCode = _ref.lookupCode,
        label = _ref.label,
        options = _ref.options,
        valueField = _ref.valueField,
        textField = _ref.textField,
        defaultValue = _ref.defaultValue;

    var optType = '';

    if (!_isUndefined(name) && !ignoreFields.includes(name)) {
      if (lookupCode || _isString(lookupUrl) || type !== 'object' && (lovCode || options)) {
        optType = 'lookup';
      }

      if (lovCode) optType = 'lov';
      return {
        value: name,
        meaning: label || name,
        type: optType || type,
        lovCode: lovCode,
        valueField: valueField,
        textField: textField,
        defaultValue: defaultValue
      };
    }

    return undefined;
  }).filter(function (item) {
    return !_isUndefined(item);
  });
  var sortDataSet = useMemo(function () {
    return new _DataSet(SortDataSet(fieldArr));
  }, []);
  var queryDataSet = useMemo(function () {
    return new _DataSet(QueryDataSet(fieldArr));
  }, []);
  var conditionDataSet = useMemo(function () {
    return new _DataSet(ConditionDataSet(tableDataSet, fieldArr));
  }, []);
  var searchDataSet = useMemo(function () {
    return new _DataSet(_objectSpread(_objectSpread({}, SearchDataSet({
      searchCode: searchCode
    })), {}, {
      children: {
        orderList: sortDataSet,
        queryList: queryDataSet,
        conditionList: conditionDataSet
      }
    }));
  }, []);

  var value = _objectSpread(_objectSpread({}, props), {}, {
    sortDataSet: sortDataSet,
    queryDataSet: queryDataSet,
    searchDataSet: searchDataSet,
    conditionDataSet: conditionDataSet,
    fieldArr: fieldArr
  });

  return /*#__PURE__*/React.createElement(Store.Provider, {
    value: value
  }, children);
};